package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    /**
     * 根据条件分页查询后台用户列表
     *
     * @param current 当前页
     * @param size    每页大小
     * @param queryVo 查询条件
     * @return 用户列表
     */
    @Override
    public IPage<SystemUserItemVo> getUserItemPageByQuery(long current, long size, SystemUserQueryVo queryVo) {
        IPage<SystemUserItemVo> page = new Page<>(current, size);
        IPage<SystemUserItemVo> systemUserItemVoIList = baseMapper.selectUserItemPageByQuery(page, queryVo);
        return systemUserItemVoIList;
    }

    /**
     * 根据ID查询后台用户信息
     *
     * @param id 用户id
     * @return 用户信息
     */
    @Override
    public SystemUserItemVo getUserItemById(Long id) {
        return baseMapper.selectUserItemById(id);
    }

    /**
     * 判断后台用户名是否存在
     *
     * @param username 用户名
     * @return 是否存在
     */
    @Override
    public Boolean usernameExists(String username) {
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUser::getUsername, username);
        return count(queryWrapper) > 0;
    }

    /**
     * 根据ID修改后台用户状态
     *
     * @param id     用户id
     * @param status 状态
     */
    @Override
    public void updateSystemUserStatus(Long id, BaseStatus status) {
        LambdaUpdateWrapper<SystemUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemUser::getId, id);
        updateWrapper.set(SystemUser::getStatus, status);
        update(updateWrapper);
    }
}




